package com.apex.aolonfitdemo.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.apex.aolonfit.AolonFitManager;
import com.apex.aolonfitdemo.R;
import com.apex.aolonfitdemo.databinding.ActivityPowerBinding;
import com.apex.aolonfitdemo.dialog.WaitingDialog;
import com.apex.ax_bluetooth.callback.BatterInfoCallback;
import com.apex.ax_bluetooth.core.EABleManager;
import com.apex.ax_bluetooth.enumeration.BatInfoStatus;
import com.apex.ax_bluetooth.enumeration.EABleConnectState;
import com.apex.ax_bluetooth.enumeration.QueryWatchInfoType;
import com.apex.ax_bluetooth.model.EABleBatInfo;
import com.cyber.bridge.module.listener.OnCyberBatteryListener;


public class PowerActivity extends AppCompatActivity {
    final String TAG = this.getClass().getSimpleName();
    private WaitingDialog waitingDialog;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }

                activityPowerBinding.powerLevel.setText(msg.arg1 + "");
                boolean charging = (boolean) msg.obj;
                if (charging) {
                    activityPowerBinding.powerState.setText("Charging");
                } else {
                    activityPowerBinding.powerState.setText("Normal");
                }

            } else if (msg.what == 0x41) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(PowerActivity.this, getString(R.string.failed_to_get_data), Toast.LENGTH_SHORT).show();
            }
        }
    };
    ActivityPowerBinding activityPowerBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityPowerBinding = ActivityPowerBinding.inflate(getLayoutInflater());
        setContentView(activityPowerBinding.getRoot());
        activityPowerBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityPowerBinding.tool);
        getSupportActionBar().setTitle(null);
        activityPowerBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        AolonFitManager.getInstance().setOnBatteryListener(new OnCyberBatteryListener() {
            @Override
            public void battery(int i, boolean b) {
                if (mHandler != null) {
                    Message message = mHandler.obtainMessage();
                    message.what = 0x40;
                    message.obj = b;
                    message.arg1 = i;
                    mHandler.sendMessage(message);
                }
            }
        });
        if (waitingDialog == null) {
            waitingDialog = new WaitingDialog(PowerActivity.this);
        }
        waitingDialog.show();
        AolonFitManager.getInstance().queryBattery();
    }

    @Override
    protected void onDestroy() {

        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }
        super.onDestroy();
    }
}
